/*****************************************************************************
*Purpose: Estimate a balance test on a sample of endogenous census 2000 movers
******************************************************************************/ 

global cenmovfe "c2000_tractid"


capture frame drop censmovers 
frame create censmovers  str35 outcome str35 spec beta se N piks treatpik controlpik dv1treatpiks dv1controlpiks dv0treatpiks dv0controlpiks

foreach boutc in female hascar employed married { 
	reghdfe `boutc' lt30black [w=pwt], absorb($cenmovfe) cluster(pik)
	unique pik if e(sample)==1
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & lt30black==1  & `boutc'==1
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & lt30black==0 & `boutc'==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & lt30black==1 & `boutc'==0
	local y0treatpik `=r(sum)'
	unique pik if e(sample)==1 & lt30black==0 & `boutc'==0
	local y0controlpik `=r(sum)'

	frame post censmovers  ("`boutc'") ("diff")  (_b[lt30black]) (_se[lt30black]) (`allpik') (`allpik') (-99999)  (-99999) (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')
	unique pik if e(sample)==1 & lt30black==0 
	local allpik `=r(sum)'
	sum `boutc' if e(sample)==1 & lt30black==0 [w=pwt]	
	frame post censmovers ("`boutc'") ("meansd") (r(mean)) (r(sd)) (`allpik') (`allpik') (-99999) (-99999) (-99999) (-99999) (-99999) (-99999)
}

foreach outc in w_inc w_wage bedneed qage mfam { 
	reghdfe `outc' lt30black [pw=pwt], absorb($cenmovfe) cluster(pik)
	unique pik if e(sample)==1	
	local allpik `=r(sum)'

	unique pik if e(sample)==1 & lt30black==1   
	local treatpiks `=r(sum)'
	unique pik if e(sample)==1 & lt30black==0  
	local controlpiks `=r(sum)'

	frame post censmovers  ("`outc'") ("diff") (_b[lt30black]) (_se[lt30black]) (`allpik') (`allpik') (`treatpiks') (`controlpiks') (-99999) (-99999) (-99999) (-99999)
	unique pik if e(sample)==1 & lt30black==0 
	local allpik `=r(sum)'
	sum `outc' if e(sample)==1 & lt30black==0  [w=pwt]
	frame post censmovers ("`outc'") ("meansd") (r(mean)) (r(sd)) (`allpik') (`allpik') (-99999) (-99999) (-99999) (-99999) (-99999) (-99999)


}

reghdfe lt30black  $c_list [pw=pwt], absorb($cenmovfe) cluster(pik)
	unique pik if e(sample)==1	
	local allpik `=r(sum)'
global pv: di %6.4fc `=Ftail(e(df_m), e(df_r), e(F))'
di $pv
di e(F)
frame post censmovers ("censusmovers") ("Fstat_pval") (e(F)) ($pv) (`allpik') (`allpik') (-99999) (-99999) (-99999) (-99999) (-99999) (-99999)

frame censmovers: save "${output}/censusmovers_results_072022.dta", replace
frame censmovers: export delimited using "${output}/censusmovers_results_072022.csv", replace



